package com.ruoyi.antchain.service;

import com.alipay.mychain.sdk.vm.EVMOutput;
import com.alipay.mychain.sdk.vm.EVMParameter;
import com.ruoyi.antchain.utils.AntChainUtils;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.controller.common.CommonController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用途
 *
 * @author deng
 * @date 2020/11/13 17:03
 */
@Service
public class VoteChainServiceImpl implements VoteChainService {
    private final Logger log = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private AntChainService antChainService;

    @Override
    public AjaxResult vote(Integer[] lan) {

        for (Integer l : lan) {
            EVMParameter parameters = new EVMParameter("voteForCandidate(bytes32)");
            parameters.addBytes32(AntChainUtils.toByte32(l));
            AjaxResult result = antChainService.callContract(VoteChainService.contractid, parameters);
            // 失败了
            if (!result.isSucccess()) {
                log.error("{},投票失败了{}", lan, result);
                return result;
            }
        }
        return AjaxResult.success();
    }

    @Override
    public TableDataInfo list() {
        TableDataInfo tableDataInfo = new TableDataInfo();
        EVMParameter parameters = new EVMParameter("candidateCount()");
        AjaxResult result = antChainService.queryContract(VoteChainService.contractid, parameters);
        if (result.isSucccess()) {
            EVMOutput output = result.getData();
            BigInteger count = output.getUint();
            List<Map<String, Object>> dataList = new ArrayList<>();
            for (int i = 1; i <= count.intValue(); i++) {
                output = result.getData();
                parameters = new EVMParameter("totalVotesFor(bytes32)");
                parameters.addBytes32(AntChainUtils.toByte32(i));
                result = antChainService.queryContract(VoteChainService.contractid, parameters);
                if (!result.isSucccess()) {
                    return tableDataInfo;
                }
                output = result.getData();
                Map<String, Object> item = new HashMap<>();
                dataList.add(item);
                item.put("index", i);
                item.put("lan", toLan(i));
                if (StringUtils.isEmpty(output.getRawOutput())) {
                    item.put("count", 0);
                } else {
                    BigInteger c = output.getUint();
                    item.put("count", c.intValue());

                }


            }
            tableDataInfo.setTotal(dataList.size());
            tableDataInfo.setRows(dataList);
        }
        return tableDataInfo;
    }

    /**
     * 转换成语言
     *
     * @param i
     * @return
     */
    private String toLan(Integer i) {
        String[] data = new String[]{"C", "java", "python", "c++", "c#", "Visual Basic", "javascript", "PHP"};
        return data[i - 1];
    }
}
